package com.ufox.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ufox.entity.Cartitem;
import com.ufox.entity.Products;
import com.ufox.entity.Result;
import com.ufox.mapper.CartitemMapper;
import com.ufox.mapper.ProductsMapper;
import com.ufox.service.ICartitemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 优狐科技-www.hbufox.com
 * @since 2022-06-09
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CartitemServiceImpl extends ServiceImpl<CartitemMapper, Cartitem> implements ICartitemService {

    @Autowired
    private CartitemMapper cartitemMapper;

    @Autowired
    private ProductsMapper productsMapper;


    @Override
    public Result addCart(String userId, String prodId) {
        //1 根据用户名 和 商品id 判断购物车中 之前该数据是否存在
        LambdaQueryWrapper<Cartitem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cartitem::getUserId, userId);
        wrapper.eq(Cartitem::getProdId, prodId);
        Cartitem cartitem = cartitemMapper.selectOne(wrapper);
        if (cartitem != null) {
            //update 原来的数量加1
            cartitem.setBuynum(cartitem.getBuynum() + 1);
            cartitemMapper.update(cartitem, wrapper);
        } else {
            //insert userid  prodid   1
            cartitemMapper.addCart(userId, prodId);
        }
        //原来数量减一
//        productsMapper.updateProdNumByIdjian(prodId);
        return Result.ok();
    }

    @Override
    public Result clearCart(String userId) {
        LambdaQueryWrapper<Cartitem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cartitem::getUserId, userId);
        cartitemMapper.delete(wrapper);
        return Result.ok();
    }

    @Override
    public Result findCartByUserId(String userId) {

        List<Products> list = new ArrayList<>();
        //获取用户购物车的每一条记录  userid  prodid  buynum
        List<Cartitem> items = cartitemMapper.findCartItemByUserId(userId);
        //2 根据prodid 查询商品详情
        for (Cartitem item : items) {
            Integer prodId = item.getProdId();
            Products products = productsMapper.findById(prodId + "", userId);
            list.add(products);
        }

        return Result.ok(list);
    }

    @Override
    public Result delCartById(String userId, String prodId, String pnum) {
        LambdaQueryWrapper<Cartitem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cartitem::getUserId, userId);
        wrapper.eq(Cartitem::getProdId, prodId);
        cartitemMapper.delete(wrapper);
        //原来数量减+删除了的数量
//        productsMapper.updateProdNumByIdjia(pnum, prodId);
        return Result.ok("删除成功", "");
    }

    @Override
    public Map<Products, Integer> findSum(String userId) {

        Map<Products, Integer> map = new HashMap<>();

        //获取用户购物车的每一条记录  userid  prodid  buynum
        List<Cartitem> items = cartitemMapper.findCartItemByUserId(userId);
        //2 根据prodid 查询商品详情
        for (Cartitem item : items) {
            int prodId = item.getProdId();
            Products products = productsMapper.findById(prodId + "", userId);
            //3 商品作为键 buynum作为值 添加到map中
            map.put(products, item.getBuynum());
        }
        return map;
    }

    @Override
    public Result delCartByIdAndInsert(String userId, String prodId, String pnum) {
        LambdaQueryWrapper<Cartitem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cartitem::getUserId, userId);
        wrapper.eq(Cartitem::getProdId, prodId);
        //全清
        cartitemMapper.delete(wrapper);
        //添加 pnum - 1
        cartitemMapper.insertCart(userId, prodId, pnum);
        //原来数量减+删除了的数量
//        productsMapper.updateProdNumByIdjia(1 + "", prodId);
        return Result.ok("删除成功", "");
    }

    @Override
    public Result getCountByUserId(String userId) {
        String count = cartitemMapper.getCountByUserId(userId);
        return Result.ok(count);
    }


}
